{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "##### Series/DataFrame/GroupBy\n",
    "##### pct_change() (Percentage change) 百分比变化\n",
    " - periods: 偏移量\n",
    " - freq: 频率(时间索引)\n",
    "  1. \"D\", \"W\", \"M\", \"MS\", \"B\"\n",
    "  2. DateOffset, Timedelta\n",
    " - limit: 最多连续填充空值个数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1\n1    2\n2    3\n3    4\ndtype: int64"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([1,2,3,4])\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "0         NaN\n1    1.000000\n2    0.500000\n3    0.333333\ndtype: float64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.pct_change()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0    NaN\n1    NaN\n2    2.0\n3    1.0\ndtype: float64"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.pct_change(periods=2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1.0\n1    2.0\n2    NaN\n3    NaN\n4    3.0\n5    4.0\ndtype: float64"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([1,2,None,None,3,4])\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "0         NaN\n1    1.000000\n2    0.000000\n3         NaN\n4         NaN\n5    0.333333\ndtype: float64"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.pct_change(limit=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "0         NaN\n1    1.000000\n2    0.000000\n3    0.000000\n4    0.500000\n5    0.333333\ndtype: float64"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.pct_change(limit=2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',\n               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',\n               '2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12',\n               '2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',\n               '2020-01-17', '2020-01-18', '2020-01-19', '2020-01-20',\n               '2020-01-21', '2020-01-22', '2020-01-23', '2020-01-24',\n               '2020-01-25', '2020-01-26', '2020-01-27', '2020-01-28',\n               '2020-01-29', '2020-01-30', '2020-01-31'],\n              dtype='datetime64[ns]', freq='D')"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.date_range(\n",
    "    '20200101',\n",
    "    '20200131',\n",
    "    freq='D'\n",
    ")\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "             A\n2020-01-01   1\n2020-01-02   2\n2020-01-03   3\n2020-01-04   4\n2020-01-05   5\n2020-01-06   6\n2020-01-07   7\n2020-01-08   8\n2020-01-09   9\n2020-01-10  10\n2020-01-11  11\n2020-01-12  12\n2020-01-13  13\n2020-01-14  14\n2020-01-15  15\n2020-01-16  16\n2020-01-17  17\n2020-01-18  18\n2020-01-19  19\n2020-01-20  20\n2020-01-21  21\n2020-01-22  22\n2020-01-23  23\n2020-01-24  24\n2020-01-25  25\n2020-01-26  26\n2020-01-27  27\n2020-01-28  28\n2020-01-29  29\n2020-01-30  30\n2020-01-31  31",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2020-01-01</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2020-01-02</th>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2020-01-03</th>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2020-01-04</th>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>2020-01-05</th>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2020-01-06</th>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2020-01-07</th>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2020-01-08</th>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>2020-01-09</th>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2020-01-10</th>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>2020-01-11</th>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>2020-01-12</th>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>2020-01-13</th>\n      <td>13</td>\n    </tr>\n    <tr>\n      <th>2020-01-14</th>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>2020-01-15</th>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>2020-01-16</th>\n      <td>16</td>\n    </tr>\n    <tr>\n      <th>2020-01-17</th>\n      <td>17</td>\n    </tr>\n    <tr>\n      <th>2020-01-18</th>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>2020-01-19</th>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>2020-01-20</th>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>2020-01-21</th>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>2020-01-22</th>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>2020-01-23</th>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>2020-01-24</th>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>2020-01-25</th>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>2020-01-26</th>\n      <td>26</td>\n    </tr>\n    <tr>\n      <th>2020-01-27</th>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2020-01-28</th>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>2020-01-29</th>\n      <td>29</td>\n    </tr>\n    <tr>\n      <th>2020-01-30</th>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>2020-01-31</th>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'A': range(1, len(index)+1)\n",
    "}, index=index)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "             A         B\n2020-01-01   1       NaN\n2020-01-02   2       NaN\n2020-01-03   3       NaN\n2020-01-04   4       NaN\n2020-01-05   5       NaN\n2020-01-06   6       NaN\n2020-01-07   7       NaN\n2020-01-08   8  7.000000\n2020-01-09   9  3.500000\n2020-01-10  10  2.333333\n2020-01-11  11  1.750000\n2020-01-12  12  1.400000\n2020-01-13  13  1.166667\n2020-01-14  14  1.000000\n2020-01-15  15  0.875000\n2020-01-16  16  0.777778\n2020-01-17  17  0.700000\n2020-01-18  18  0.636364\n2020-01-19  19  0.583333\n2020-01-20  20  0.538462\n2020-01-21  21  0.500000\n2020-01-22  22  0.466667\n2020-01-23  23  0.437500\n2020-01-24  24  0.411765\n2020-01-25  25  0.388889\n2020-01-26  26  0.368421\n2020-01-27  27  0.350000\n2020-01-28  28  0.333333\n2020-01-29  29  0.318182\n2020-01-30  30  0.304348\n2020-01-31  31  0.291667",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2020-01-01</th>\n      <td>1</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-02</th>\n      <td>2</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-03</th>\n      <td>3</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-04</th>\n      <td>4</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-05</th>\n      <td>5</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-06</th>\n      <td>6</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-07</th>\n      <td>7</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-08</th>\n      <td>8</td>\n      <td>7.000000</td>\n    </tr>\n    <tr>\n      <th>2020-01-09</th>\n      <td>9</td>\n      <td>3.500000</td>\n    </tr>\n    <tr>\n      <th>2020-01-10</th>\n      <td>10</td>\n      <td>2.333333</td>\n    </tr>\n    <tr>\n      <th>2020-01-11</th>\n      <td>11</td>\n      <td>1.750000</td>\n    </tr>\n    <tr>\n      <th>2020-01-12</th>\n      <td>12</td>\n      <td>1.400000</td>\n    </tr>\n    <tr>\n      <th>2020-01-13</th>\n      <td>13</td>\n      <td>1.166667</td>\n    </tr>\n    <tr>\n      <th>2020-01-14</th>\n      <td>14</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>2020-01-15</th>\n      <td>15</td>\n      <td>0.875000</td>\n    </tr>\n    <tr>\n      <th>2020-01-16</th>\n      <td>16</td>\n      <td>0.777778</td>\n    </tr>\n    <tr>\n      <th>2020-01-17</th>\n      <td>17</td>\n      <td>0.700000</td>\n    </tr>\n    <tr>\n      <th>2020-01-18</th>\n      <td>18</td>\n      <td>0.636364</td>\n    </tr>\n    <tr>\n      <th>2020-01-19</th>\n      <td>19</td>\n      <td>0.583333</td>\n    </tr>\n    <tr>\n      <th>2020-01-20</th>\n      <td>20</td>\n      <td>0.538462</td>\n    </tr>\n    <tr>\n      <th>2020-01-21</th>\n      <td>21</td>\n      <td>0.500000</td>\n    </tr>\n    <tr>\n      <th>2020-01-22</th>\n      <td>22</td>\n      <td>0.466667</td>\n    </tr>\n    <tr>\n      <th>2020-01-23</th>\n      <td>23</td>\n      <td>0.437500</td>\n    </tr>\n    <tr>\n      <th>2020-01-24</th>\n      <td>24</td>\n      <td>0.411765</td>\n    </tr>\n    <tr>\n      <th>2020-01-25</th>\n      <td>25</td>\n      <td>0.388889</td>\n    </tr>\n    <tr>\n      <th>2020-01-26</th>\n      <td>26</td>\n      <td>0.368421</td>\n    </tr>\n    <tr>\n      <th>2020-01-27</th>\n      <td>27</td>\n      <td>0.350000</td>\n    </tr>\n    <tr>\n      <th>2020-01-28</th>\n      <td>28</td>\n      <td>0.333333</td>\n    </tr>\n    <tr>\n      <th>2020-01-29</th>\n      <td>29</td>\n      <td>0.318182</td>\n    </tr>\n    <tr>\n      <th>2020-01-30</th>\n      <td>30</td>\n      <td>0.304348</td>\n    </tr>\n    <tr>\n      <th>2020-01-31</th>\n      <td>31</td>\n      <td>0.291667</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['B'] = df['A'].pct_change(\n",
    "    freq='7D'\n",
    ")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "             A         B\n2020-01-01   1       NaN\n2020-01-02   2  1.000000\n2020-01-03   3  0.500000\n2020-01-04   4       NaN\n2020-01-05   5       NaN\n2020-01-06   6  1.000000\n2020-01-07   7  0.166667\n2020-01-08   8  0.142857\n2020-01-09   9  0.125000\n2020-01-10  10  0.111111\n2020-01-11  11       NaN\n2020-01-12  12       NaN\n2020-01-13  13  0.300000\n2020-01-14  14  0.076923\n2020-01-15  15  0.071429\n2020-01-16  16  0.066667\n2020-01-17  17  0.062500\n2020-01-18  18       NaN\n2020-01-19  19       NaN\n2020-01-20  20  0.176471\n2020-01-21  21  0.050000\n2020-01-22  22  0.047619\n2020-01-23  23  0.045455\n2020-01-24  24  0.043478\n2020-01-25  25       NaN\n2020-01-26  26       NaN\n2020-01-27  27  0.125000\n2020-01-28  28  0.037037\n2020-01-29  29  0.035714\n2020-01-30  30  0.034483\n2020-01-31  31  0.033333",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2020-01-01</th>\n      <td>1</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-02</th>\n      <td>2</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>2020-01-03</th>\n      <td>3</td>\n      <td>0.500000</td>\n    </tr>\n    <tr>\n      <th>2020-01-04</th>\n      <td>4</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-05</th>\n      <td>5</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-06</th>\n      <td>6</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>2020-01-07</th>\n      <td>7</td>\n      <td>0.166667</td>\n    </tr>\n    <tr>\n      <th>2020-01-08</th>\n      <td>8</td>\n      <td>0.142857</td>\n    </tr>\n    <tr>\n      <th>2020-01-09</th>\n      <td>9</td>\n      <td>0.125000</td>\n    </tr>\n    <tr>\n      <th>2020-01-10</th>\n      <td>10</td>\n      <td>0.111111</td>\n    </tr>\n    <tr>\n      <th>2020-01-11</th>\n      <td>11</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-12</th>\n      <td>12</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-13</th>\n      <td>13</td>\n      <td>0.300000</td>\n    </tr>\n    <tr>\n      <th>2020-01-14</th>\n      <td>14</td>\n      <td>0.076923</td>\n    </tr>\n    <tr>\n      <th>2020-01-15</th>\n      <td>15</td>\n      <td>0.071429</td>\n    </tr>\n    <tr>\n      <th>2020-01-16</th>\n      <td>16</td>\n      <td>0.066667</td>\n    </tr>\n    <tr>\n      <th>2020-01-17</th>\n      <td>17</td>\n      <td>0.062500</td>\n    </tr>\n    <tr>\n      <th>2020-01-18</th>\n      <td>18</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-19</th>\n      <td>19</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-20</th>\n      <td>20</td>\n      <td>0.176471</td>\n    </tr>\n    <tr>\n      <th>2020-01-21</th>\n      <td>21</td>\n      <td>0.050000</td>\n    </tr>\n    <tr>\n      <th>2020-01-22</th>\n      <td>22</td>\n      <td>0.047619</td>\n    </tr>\n    <tr>\n      <th>2020-01-23</th>\n      <td>23</td>\n      <td>0.045455</td>\n    </tr>\n    <tr>\n      <th>2020-01-24</th>\n      <td>24</td>\n      <td>0.043478</td>\n    </tr>\n    <tr>\n      <th>2020-01-25</th>\n      <td>25</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-26</th>\n      <td>26</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2020-01-27</th>\n      <td>27</td>\n      <td>0.125000</td>\n    </tr>\n    <tr>\n      <th>2020-01-28</th>\n      <td>28</td>\n      <td>0.037037</td>\n    </tr>\n    <tr>\n      <th>2020-01-29</th>\n      <td>29</td>\n      <td>0.035714</td>\n    </tr>\n    <tr>\n      <th>2020-01-30</th>\n      <td>30</td>\n      <td>0.034483</td>\n    </tr>\n    <tr>\n      <th>2020-01-31</th>\n      <td>31</td>\n      <td>0.033333</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['B'] = df['A'].pct_change(\n",
    "    freq='B'\n",
    ")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}